package com.rigsofrods.truckeditor.gui.views;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.datatransfer.Transferable;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.TransferHandler;

import com.rigsofrods.truckeditor.rig.TruckFileModel;

public class TruckTreeView extends JPanel{
	private static final long serialVersionUID = -6410372084538289178L;

	private static final Dimension MIN_SIZE = new Dimension(100, 60);
	
	private JScrollPane jsp;
	private TruckFileModel truckFileTree;
	private JTree jTree;
	//private TreeModel tm;
	//Vsechny akce sem
	//vyrobit menu(+popup)
	//Drag n drop text?
	//private UndoManager undoManager;
	//private UndoAction undoAction;
	//class UndoAction extends AbstractAction;

	public TruckTreeView() {
		super(new GridLayout(1,0));
	}
	
	public void setTruckFile(TruckFileModel truckFile){
		truckFileTree = truckFile;
		//Remove old scroll pane and create a new one with a new jtree.
		if(jTree == null){
			if (jsp != null)
				remove(jsp);
			//Re-register listeners?
			//EventListener[] el = jTree.getListeners(EventListener.class);
			jTree = new JTree(truckFile);
			jTree.setVisibleRowCount(20);
			jTree.setExpandsSelectedPaths(true);
			jsp = new JScrollPane(jTree);
			jsp.setPreferredSize(getSize());
			jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
			add(jsp);
		} else {
			jTree.setModel(truckFile);
		}
		revalidate();//To repaint this panel after truck file has been set
	}

	@Override
	public Dimension getMinimumSize(){
		return MIN_SIZE;		
	}

	public TruckFileModel getTruckFile() {
		return truckFileTree;
	}
	
	public JTree getjTree() {
		return jTree;
	}
}

class TruckSectionTransferHandler extends TransferHandler {
	private static final long serialVersionUID = -7427165129341098235L;

	public TruckSectionTransferHandler() {
		super();
	}
	
	//export:
	@Override
	public int getSourceActions(JComponent c) {
	    return COPY_OR_MOVE;
	}

	@Override
	public Transferable createTransferable(JComponent c) {
	    return null;
	}

	@Override
	public void exportDone(JComponent c, Transferable t, int action) {
	    if (action == MOVE) {
	        //c.removeSelection();
	    }
	}
	
	@Override
	public boolean canImport(TransferSupport supp){
		return false;
	}

	//import
	@Override
	public boolean importData(TransferSupport supp){
		return false;
	}
	
}